package com.hj.security.adapter;

import com.hj.security.R;
import com.hj.security.R.drawable;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainUIAdapter extends BaseAdapter {

	private static final String[] NAMES = new String[] { "手机防盗", "通讯卫士",
			"软件管理", "流量管理", "任务管理", "手机杀毒", "系统优化", "高级工具", "设置中心" };
	private static final int[] ICONS = new int[] { R.drawable.widget01,
			R.drawable.widget02, R.drawable.widget03, R.drawable.widget04,
			R.drawable.widget05, R.drawable.widget06, R.drawable.widget07,
			R.drawable.widget08, R.drawable.widget09 };
	
	private static ImageView imageView;
	private static TextView textView;
	
	private Context context;
	private LayoutInflater layoutInflater;
	private SharedPreferences sp;

	public MainUIAdapter(Context context) {
		this.context=context;
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return NAMES.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//1、View view = View.inflate(context, R.layout.main_item, null);
		View view;
		ImageView iv_main_icon;
		TextView tv_main_name;
		if(convertView==null){
			view = layoutInflater.inflate(R.layout.main_item, null);
			iv_main_icon = (ImageView) view.findViewById(R.id.iv_main_icon);
			tv_main_name = (TextView) view.findViewById(R.id.tv_main_name);
			iv_main_icon.setImageResource(ICONS[position]);
			tv_main_name.setText(NAMES[position]);			
		}else{
			view = convertView;
			iv_main_icon = (ImageView) view.findViewById(R.id.iv_main_icon);
			tv_main_name = (TextView) view.findViewById(R.id.tv_main_name);
			iv_main_icon.setImageResource(ICONS[position]);
			tv_main_name.setText(NAMES[position]);	
		}

		if(position==0){
			String name = sp.getString("lostName", "");
			if(!"".equals(name)){
				tv_main_name.setText(name);
			}
		}
		return view;
	}

}
